package com.ld.security.common.core.util;



import cn.hutool.core.lang.Console;
import lombok.SneakyThrows;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;


/**
 * AES加解密工具
 * AES-128: key和iv都是16个字节，16*8=128bit，java似乎只支持AES-128
 */
public class AesCrypt {


    private static String defIv ="1234567890000000";
    private static String defKey="1234567890123456";

    private static String cipherMode="AES/CBC/PKCS5Padding";
    private static String charsetName="UTF-8";

    private static String aesName="AES";



    /**
     * AES CBC 加密
     * @param message 需要加密的字符串
     * @param key   密匙
     * @param iv    IV，需要和key长度相同
     * @return  返回加密后密文，编码为base64
     */
    @SneakyThrows
    public static String encrypt(String message, String key, String iv) {


            byte[] content = message.getBytes(charsetName);
            //
            byte[] keyByte = key.getBytes(charsetName);
            SecretKeySpec keySpec = new SecretKeySpec(keyByte, aesName);
            //
            byte[] ivByte = iv.getBytes(charsetName);
            IvParameterSpec ivSpec = new IvParameterSpec(ivByte);

            Cipher cipher = Cipher.getInstance(cipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] data = cipher.doFinal(content);
            final Base64.Encoder encoder = Base64.getEncoder();
            final String result = encoder.encodeToString(data);
            return result;


    }

  public static String encrypt(String message) {
      return encrypt(message,defKey,defIv);
  }

  public static String decrypt(String messageBase64) {
        return decrypt(messageBase64,defKey,defIv);
  }
        /**
         * AES CBC 解密
         * @param messageBase64 密文，base64编码
         * @param key   密匙，和加密时相同
         * @param iv    IV，需要和key长度相同
         * @return  解密后数据
         */
    @SneakyThrows
    public static String decrypt(String messageBase64, String key, String iv) {


            final Base64.Decoder decoder = Base64.getDecoder();
            byte[] messageByte = decoder.decode(messageBase64);

            //
            byte[] keyByte = key.getBytes(charsetName);
            SecretKeySpec keySpec = new SecretKeySpec(keyByte, aesName);
            //
            byte[] ivByte = iv.getBytes(charsetName);
            IvParameterSpec ivSpec = new IvParameterSpec(ivByte);

            Cipher cipher = Cipher.getInstance(cipherMode);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] content = cipher.doFinal(messageByte);
            String result = new String(content, charsetName);
            return result;


    }



}
